package org.meb.conquest.web.model;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import lombok.Getter;

import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.MultiMap;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.map.MultiValueMap;

public class CardGroup {

	@Getter
	private MultiMap<String, CardItem> byType;
	private Map<String, Integer> byTypeCount;
	private Transformer<CardItem, String> keyTransformer = new Transformer<CardItem, String>() {

		@Override
		public String transform(CardItem input) {
			return input.getCard().getType().toString();
		}
	};
	private Transformer<CardItem, CardItem> valueTransformer = new Transformer<CardItem, CardItem>() {

		@Override
		public CardItem transform(CardItem input) {
			return input;
		}
	};

	// private Transformer<CardItem, CardItem> quantityTransformer = new
	// Transformer<CardItem, CardItem>() {
	//
	// };

	public void groupByType(List<CardItem> cards) {
		if (byType == null) {
			byType = new MultiValueMap<>();
		} else {
			byType.clear();
		}

		if (cards == null) {
			return;
		}
		Collection<CardItem> filteredCards = CollectionUtils.select(cards, new Predicate<CardItem>() {

			@Override
			public boolean evaluate(CardItem object) {
				return object.getQuantity() > 0;
			}

		});
		MapUtils.populateMap(byType, filteredCards, keyTransformer, valueTransformer);
	}

}
